const app = getApp();
import {
  myOrder,
  cancelOrder,
  deleteOrder,
  receiveOrder,
  paymentAlign
} from "../../../server/api";
import { parseTime } from '../../../utils/util';
Page({
  data: {
    oss_url: app.globalData.oss_url,
    list: [],
    serviceShow:false,
    expressShow:false,
    warnShow:false,
    deleteShow:false,
    buttons:[{text: '删除'}, {text: '取消'}],
    tabs:['全部','待支付','待发货','待收货','已完成'],  //tabs
    tabIndex:0,
    status:'',
    btns:[],
    orderId:''
  },
  onLoad: function (options) {
    this.orderList();
  },
  orderList(){
    let status=this.data.tabIndex;
    if(status==0){
      status=''
    }
    else if(status==4){
      status=5
    }
    let data={
      oneId:app.globalData.user_info.oneId,
      orderStatus:status
    }
    myOrder(data).then(res=>{
      if(res.code==0){
        let list=res.data.list;
        list.forEach((item)=>{
          let orderTime=parseTime(item.createTime,'{y}-{m}-{d} {h}:{i}');
          let payTime=parseTime(item.paymentTime,'{y}-{m}-{d} {h}:{i}');
          let receiveTime=parseTime(item.receiveTime,'{y}-{m}-{d} {h}:{i}');
          let cancelTime=parseTime(item.cancelTime,'{y}-{m}-{d} {h}:{i}');
          item.createTime=orderTime;
          item.paymentTime=payTime;
          item.receiveTime=receiveTime;
          item.cancelTime=cancelTime;
        })
        this.setData({
          list
        })

      }
    })
  },
  clickTab(e){
    let index=e.currentTarget.dataset.index;
    // if(index==4){
    //   index=5
    // }
    this.setData({
      tabIndex:index
    })
    this.orderList()
  },
  copy(e){
    wx.setClipboardData({
      data: e.currentTarget.dataset.code,
      success: function (res) {
        wx.showToast({
          title: '复制成功',
          icon:'none'
        });
      }
    });
  },
  handleBtn(e){
    let that=this;
    let btn=e.detail.item.text;
    console.log(btn)
    if(btn=='取消'){
      this.setData({
        deleteShow:false
      })
    }else if(btn=='删除'){
      //删除订单
      let data={
        orderId:this.data.orderId
      }
      console.log(data)
      deleteOrder(data).then(res=>{
        if(res.code==0){
          wx.showToast({
            title: '订单已删除',
            icon: 'none',
            duration: 1000
          })
          that.setData({
            deleteShow:false
          })
          that.orderList()
        }
        else{
          wx.showToast({
            title: res.errMsg,
            icon: 'none'
          })
        }
      })
    } 
  },
   //操作订单
  handleOrder(e){
    let that=this;
    let { type,id,goodsid ,payamount,goodsname}=e.currentTarget.dataset;
    switch(type){
      case 'cancel':
        let data={
          orderId:id
        }
        cancelOrder(data).then(res=>{
          if(res.code==0){
            wx.showToast({
              title: '订单已取消',
              icon: 'none',
              duration: 1000
            })
            that.orderList()
          }
          else{
            wx.showToast({
              title: res.errMsg,
              icon: 'none'
            })
          }
        })
      break;
      case 'pay':
        //跳转至商品详情去支付，订单列表没有收货地址信息
        // wx.navigateTo({
        //   url: '/pages/goodsOrder/goodsDetail/index?id'+goodsid,
        // })
        let payData={
          goodsId:goodsid,
          orderId:id,
          payAmount:payamount,
          goodsName:goodsname,
          appId:'wxe4299f541f78a49e',
          openId:app.globalData.user_info.openId
        }
        console.log(payData);
        paymentAlign(payData).then(res=>{
          if(res.code==0){
            this.goPay(res)
          }
        })
      break;
      //提醒发货
      case 'fahuo':
        this.setData({
          btns:[{text: '知道了'}],
          warnShow:true
        })
      break;
      //确认收货
      case 'takeGoods':
        let datas={
          orderId:id
        };
        receiveOrder(datas).then(res=>{
          if(res.code==0){
            wx.showToast({
              title: '收货完成',
              icon: 'none'
            })
            that.orderList()
          }
          else{
            wx.showToast({
              title: res.errMsg,
              icon: 'none'
            })
          }
        })
      break;
      //联系客服
      case 'service':
        this.setData({
          btns:[{text: '取消'}, {text: '拨打电话'}],
          serviceShow:true
        })
      break;
      //删除订单
      case 'delete':
        this.setData({
          deleteShow:true,
          orderId:id
        })
      break;
      //查看物流
      case 'express':   
        this.setData({
          btns:[{text: '取消'}, {text: '知道了'}],
          expressShow:true
        })
      break;
    }
  },
  goPay(res){
    wx.showToast({
      title: '支付请求中',
      icon: 'loading',
      duration: 3000
    })
    wx.requestPayment(
      {
        'timeStamp': res.data.timeStamp,
        'nonceStr': res.data.nonceStr,
        'package': res.data.package,
        'signType': res.data.signType,
        'paySign': res.data.paySign,
        'success': function (res) {
          console.log(res);
          wx.showToast({
            title: '支付成功',
            icon: 'none',
          })
          // 成功之后跳转-我的订单 页
          wx.navigateTo({
            url: "/pages/goodsOrder/orderList/index",
          })
        },
        'fail': function (res) {
          wx.showLoading({ title: "支付错误,请重新支付" });
          setTimeout(function () { wx.hideLoading() }, 1000);
        },
        'complete': function (res) {
          setTimeout(function () { wx.hideLoading() }, 1000);
        }
      })
  }
})